package com.bawei.live.fragment

import android.annotation.SuppressLint
import android.os.Bundle
import android.os.CountDownTimer
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.bawei.base.RoutePath
import com.bawei.common.mvi.BaseMviFragment
import com.bawei.live.R
import com.bawei.live.adapter.GloBalFlashAdapter
import com.bawei.live.adapter.GloBalFlashTypeAdapter
import com.bawei.live.databinding.FragmentGlobalFlashBinding
import com.bawei.live.entity.UserEntity
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.tencent.mmkv.MMKV
import dagger.hilt.android.AndroidEntryPoint


/**
 * Desc:
 */
@Route(path = RoutePath.PATH_FRAGMENT_GLOBAL_COMMEND)
class GlobalFlashFragment : BaseMviFragment<FragmentGlobalFlashBinding>() {

    private lateinit var countDownTimer:CountDownTimer //实现倒计时
    private val totalTimeInMillis:Long = 60 * 60 * 1000 //60:00 转换为毫米哦

    private lateinit var mUserAdapter:GloBalFlashAdapter //适配器
    private val userlist = mutableListOf<UserEntity>()

    private lateinit var mUserAdapterType:GloBalFlashTypeAdapter
    private val userlistType = mutableListOf<UserEntity>()


    override fun getLayoutId(): Int = R.layout.fragment_global_flash



    override fun initViews() {
//        mDataBinding.tvAward.setOnClickListener{
//            mDataBinding.cltAwardRules.visibility = View.VISIBLE
//
//        }
//        mDataBinding.ivGoBack.setOnClickListener{
//            mDataBinding.cltAwardRules.visibility = View.GONE
//        }

        //倒计时
        CountDownTime()
        //添加用户
        userlist.add(UserEntity(0,4,"","条条哥超级买手","",0,false))
        userlist.add(UserEntity(0,5,"","萝莉女装","",0,false))
        userlist.add(UserEntity(0,7,"","虾虾","",0,false))
        userlist.add(UserEntity(0,8,"","高齐强","",0,false))
        userlist.add(UserEntity(0,9,"","高齐升","",0,false))
        userlist.add(UserEntity(0,10,"","路飞","",0,false))
        userlist.add(UserEntity(0,11,"","蔡坤","",0,false))
        userlist.add(UserEntity(0,12,"","熊猫元","",0,false))
        userlist.add(UserEntity(0,13,"","花花","",0,false))
        userlist.add(UserEntity(0,14,"","草草","",0,false))

        //添加排名用户
        userlistType.add(UserEntity(0,2,"","产品研究所","",0,false))
        userlistType.add(UserEntity(0,1,"","熊猫大哥","",0,false))
        userlistType.add(UserEntity(0,3,"","我勒个狮子王","",0,false))




        mUserAdapter = GloBalFlashAdapter(userlist)
        mUserAdapterType = GloBalFlashTypeAdapter(userlistType)

        mDataBinding.rvFlash.run {
            layoutManager = LinearLayoutManager(requireContext(),LinearLayoutManager.VERTICAL,false)
            swapAdapter(mUserAdapter,true)//开启屏幕外缓存
        }

        mDataBinding.rvFlashType.run {
            layoutManager =
                LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false)
            swapAdapter(mUserAdapterType, true)



            mUserAdapter.setOnItemClickListener(object : GloBalFlashAdapter.OnItemClickListener {
                override fun onItemClick(v: View, positions: Int) {
                    mDataBinding.textView2.text = userlist[positions].userName
                    mDataBinding.textView.text = userlist[positions].textCount.toString()
                    ToastUtils.showLong("关注成功")
                }
            })


            mUserAdapterType.setOnItemClickListener(object:GloBalFlashTypeAdapter.OnItemClickListener{
                override fun onItemClick(v: View, positions: Int) {
                    mDataBinding.textView2.text = userlistType[positions].userName
                    mDataBinding.textView.text = userlistType[positions].textCount.toString()
                    ToastUtils.showLong("关注成功")
                }
            })


        }
    }
    private fun CountDownTime(){
        countDownTimer = object:CountDownTimer(totalTimeInMillis,1000){
            override fun onTick(p: Long) {
                val minutes = p / (60 * 1000)
                val seconds = (p % (60 * 1000))/1000
                val timeFormat = String.format("%02d:%02d", minutes, seconds)
                mDataBinding.tvCountdown.text = timeFormat
            }

            override fun onFinish() {
                ToastUtils.showLong("结束")
            }
        }.start()
    }






}